﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SurvivalMan
{
    /// <summary>
    /// This is the Collision class that checks if two pictures are in eachother.
    /// </summary>
    class Collision
    {
        /// <summary>
        /// This is the function that checks if two pictures have touched eachother.
        /// The function need the rectangle and the color(vector Color) from each of the two pictures.
        /// This function checks collision per pixel so if you are using some part of transparent pictures it will only register collision by the visual parts.
        /// </summary>
        /// <param name="rectangleA"></param>
        /// <param name="dataA"></param>
        /// <param name="rectangleB"></param>
        /// <param name="dataB"></param>
        /// <returns></returns>
        public bool kollision(Rectangle rectangleA, Color[] dataA, Rectangle rectangleB, Color[] dataB)
        {
            //hitta de delar av rektanglarna som överlappar
            int top = Math.Max(rectangleA.Top, rectangleB.Top);
            int bot = Math.Min(rectangleA.Bottom, rectangleB.Bottom);
            int left = Math.Max(rectangleA.Left, rectangleB.Left);
            int right = Math.Min(rectangleA.Right, rectangleB.Right);

            //kollar varje pixel om de är transparenta och icke överlappande
            for (int y = top; y < bot; y++)
            {
                for (int x = left; x < right; x++)
                {
                    Color colorA = dataA[(x - rectangleA.Left) + (y - rectangleA.Top) * rectangleA.Width];
                    Color colorB = dataB[(x - rectangleB.Left) + (y - rectangleB.Top) * rectangleB.Width];
                    //kollar ifall dessa är icke transparenta
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        return true;
                    }
                }
            }


            return false;
        }
    }
}
